package com.zwy.fornote.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zwy.fornote.dao.QuestionMapper;
import com.zwy.fornote.entity.req.NewQuestionReq;
import com.zwy.fornote.entity.req.QuestionReq;
import com.zwy.fornote.entity.vo.QuestionManageVO;
import com.zwy.fornote.model.Question;
import com.zwy.fornote.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class QuestionServiceImpl implements QuestionService {

    @Autowired
    QuestionMapper questionMapper;

    @Override
    public int createNewQuestion(NewQuestionReq req) {
        Question question=new Question();
        question.setContent(req.getContent());
        //todo 替换为工具类获取当前用户
        question.setUserId(req.getUserId());
//        question.setUserId(UserThreadLocal.get().getId());
        question.setDeleted(0);
        question.setBrowseNum(0);
        return questionMapper.insertSelective(question);
    }

    @Override
    public int deleteQuestion(int quesId) {
        Question question=questionMapper.selectByPrimaryKey(quesId);
        question.setDeleted(1);
        return questionMapper.updateByPrimaryKeySelective(question);

    }

    @Override
    public int AddQuestionBrowseNum(int quesId) {
        Question question=questionMapper.selectByPrimaryKey(quesId);
        question.setBrowseNum(question.getBrowseNum()+1);
        return questionMapper.updateByPrimaryKeySelective(question);
    }

    @Override
    public PageInfo<Question> selectQuestions(QuestionReq req) {
        PageHelper.startPage(req.getPageNum(),req.getPageSize());
        List<Question> questions = questionMapper.selectQuestion(req.getContent());
        PageInfo<Question> pageInfo=new PageInfo<>(questions);
        return pageInfo;
    }

    @Override
    public PageInfo<QuestionManageVO> selectQuestionManageList(QuestionReq req) {
        PageHelper.startPage(req.getPageNum(),req.getPageSize());
        List<QuestionManageVO> questions = questionMapper.selectManageQuestion(req.getContent());
        PageInfo<QuestionManageVO> pageInfo=new PageInfo<>(questions);
        return pageInfo;
    }
}
